package com.oliet.security;

import cn.hutool.core.lang.Assert;
import com.oliet.entity.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * @author jiaZhenpeng
 * @Title: AccountUser
 * @Package com.oliet.security
 * @Description:
 * @date 2023-11-28 15:21
 */
@Slf4j
public class AccountUser implements UserDetails {

    private static final long serialVersionUID = 540L;
    private Long userId;
    private String code;
    private String password;
    private String fullName;
    private final String username;
    private final Collection<? extends GrantedAuthority> authorities;
    private final boolean accountNonExpired;
    private final boolean accountNonLocked;
    private final boolean credentialsNonExpired;
    private final boolean enabled;

    public AccountUser(Users sysUser, Collection<? extends GrantedAuthority> authorities) {
        this(sysUser, true, true, true, true, authorities);
    }

    public AccountUser(Users sysUser, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        Assert.isTrue(sysUser.getUsername() != null && !"".equals(sysUser.getUsername()) && sysUser.getPassword() != null, "Cannot pass null or empty values to constructor");
        this.userId = sysUser.getId();
        this.code = sysUser.getCode();
        this.username = sysUser.getUsername();
        this.password = sysUser.getPassword();
        this.fullName = sysUser.getFullName();
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = authorities;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    public Long getUserId() {
        return userId;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    public String getFullName() {
        return fullName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
